package com.lovo.stock.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.stock.bean.*;
import com.lovo.stock.constant.MessageCode;
import com.lovo.stock.service.IUserService;
import com.lovo.stock.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户控制层
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;

    /**
     * 分页查询用户
     * @param pageInfo
     * @return
     */
    @RequestMapping("/queryUserByPage")
    public ResponseResult<IPage<UserBean>> queryUserByPage(@RequestBody PageInfo<UserBean> pageInfo){
        ResponseResult<IPage<UserBean>> responseResult=new ResponseResult<>();
        try {
            responseResult.setData(userService.queryUserByPage(pageInfo));
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 新增用户
     * @param roleIds
     *  @param user
     */
    @RequestMapping("/addUser")
    public ResponseResult addUser(String roleIds,String user){
        List<Integer> list=JSONObject.parseArray(roleIds, Integer.class);
        UserBean userBean=JSONObject.parseObject(user, UserBean.class);
        userBean.setUserPassword("123456");
        ResponseResult responseResult=new ResponseResult<>();
        try {
            userService.addUser(userBean,list);
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 根据id查询用户
     * @param userId
     * @return
     */
    @RequestMapping("/queryUserById")
    public ResponseResult<UserBean> queryUserById(int userId){
        ResponseResult<UserBean> responseResult=new ResponseResult<>();
        try {
            responseResult.setData(userService.queryUserById(userId));
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 修改用户角色
     * @param roleIds
     * @param userId
     */
    @RequestMapping("/updateUser")
    public ResponseResult updateUser(String roleIds, Integer userId){
        List<Integer> roleId=JSONObject.parseArray(roleIds,Integer.class);
        ResponseResult responseResult=new ResponseResult<>();
        try {
            userService.updateUser(roleId,userId);
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping("/deleteUser")
    public ResponseResult deleteUser(int id){
        ResponseResult responseResult=new ResponseResult<>();
        try {
            userService.deleteUser(id);
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 修改用户密码
     * @param old
     * @param newU
     * @return
     */
    @RequestMapping("/updateUserPassword")
    public ResponseResult updateUserPassword(HttpServletRequest request,String old, String newU){
        ResponseResult responseResult=new ResponseResult<>();
        UserBean user= (UserBean) request.getSession().getAttribute("user");
        UserBean oldUser = JSONObject.parseObject(old,UserBean.class);
        oldUser.setUserId(user.getUserId());
        UserBean newUser=JSONObject.parseObject(newU,UserBean.class);
        newUser.setUserId(user.getUserId());
        try {
            userService.updateUserPassword(oldUser, newUser);
            //删除会话对象中的user
            request.getSession().removeAttribute("user");
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

}
